package com.example.sunlight.wzoneunicom;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;


import com.example.sunlight.wzoneunicom.Origin;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created by SunLight on 2017/7/20.
 */

public class Views extends View {
    int[] i;
    boolean[] b;
    String[] level;
    Context context;
    Origin origin;
    static MoveTo moveTo;
    static boolean boo = false;
    public static int[][] end;


    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0x123:
                    Views.this.invalidate();
                    break;
                case 0x789:
                    Views.this.invalidate();
                    break;
            }
        }
    };

    ChildMove childMove;
    int[] save;

    public Views(Context context, String[] obm, int[] save, Activity activity, Origin origin) {
        super(context);
        this.context = context;
        this.level = obm;
        this.save = save;
        this.origin = origin;
        i = new int[obm.length];
        b = new boolean[obm.length];
        for (int j = 0; j < obm.length; j++) {
            new RingSize(j, i, b, obm).start();
        }
        end = origin.ZigbeeStellate;
        moveTo = new MoveTo();
        moveTo.start();
        childMove = new ChildMove();
        childMove.start();
    }

    public Views(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public Views(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    public static int kk = 0;
    int ss = 0x55BFC1C3;
    int tt = 7;
    int du = 0;
    int kit1 = Color.argb(200, 0, 163, 255);

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.translate((getWidth() + getPaddingLeft() - getPaddingRight()) / 2,
                (getHeight() + getPaddingTop() - getPaddingBottom()) / 2);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStrokeWidth(1);
        paint.setColor(ss);
        if (kk == 1 || kk == 2) {
            canvas.rotate(0, 0, 0);
        } else {
            canvas.rotate(du, 0, 0);
        }
        for (int j = 0; j < origin.ZigbeeStellate.length; j++) {
            for (int k = 0; k < 1; k++) {
                if (j == 0) {
                    paint.setColor(Color.WHITE);
                    canvas.drawCircle(origin.ZigbeeStellate[0][0], origin.ZigbeeStellate[0][1], 6, paint);
                } else {
                    paint.setColor(ss);
                    canvas.drawCircle(origin.ZigbeeStellate[j][k], origin.ZigbeeStellate[j][k + 1], 5 + i[j] + save[j], paint);
                    if (save[j] != 0) {
                        paint.setColor(kit1);
                        canvas.drawCircle(origin.ZigbeeStellate[j][k], origin.ZigbeeStellate[j][k + 1], 5 + save[j], paint);
//                        origin.ZigbeeStellate[j][k]+=10;
//                        origin.ZigbeeStellate[j][k + 1]+=10;
                    } else {
                        paint.setColor(Color.WHITE);
                        canvas.drawCircle(origin.ZigbeeStellate[j][k], origin.ZigbeeStellate[j][k + 1], 5 + save[j], paint);
//                    canvas.drawText(j + "", origin.ZigbeeStellate[j][k], origin.ZigbeeStellate[j][k + 1], paint);

                    }

                }
                /**
                 * 星状图(画线)
                 * */
                paint.setColor(ss);
                if (kk == 0) {
                    canvas.drawLine(origin.ZigbeeStellate[0][0], origin.ZigbeeStellate[0][1], origin.ZigbeeStellate[j][0], origin.ZigbeeStellate[j][1], paint);
                }
                /**
                 * 树形图(连线)
                 * */
                else if (kk == 1) {
                    if (j >= 1 && j <= 8) {
                        canvas.drawLine(origin.ZigbeeStellate[0][0], origin.ZigbeeStellate[0][1], origin.ZigbeeStellate[j][0], origin.ZigbeeStellate[j][1], paint);
                    }
                    if (j >= 9 && j <= 10) {
                        canvas.drawLine(origin.ZigbeeStellate[8][0], origin.ZigbeeStellate[8][1], origin.ZigbeeStellate[j][0], origin.ZigbeeStellate[j][1], paint);
                    }
                    if (j >= 11 && j <= 12) {
                        canvas.drawLine(origin.ZigbeeStellate[6][0], origin.ZigbeeStellate[6][1], origin.ZigbeeStellate[j][0], origin.ZigbeeStellate[j][1], paint);
                    }
                    if (j >= 13 && j <= 14) {
                        canvas.drawLine(origin.ZigbeeStellate[4][0], origin.ZigbeeStellate[4][1], origin.ZigbeeStellate[j][0], origin.ZigbeeStellate[j][1], paint);
                    }
                    if (j >= 15 && j <= 16) {
                        canvas.drawLine(origin.ZigbeeStellate[2][0], origin.ZigbeeStellate[2][1], origin.ZigbeeStellate[j][0], origin.ZigbeeStellate[j][1], paint);
                    }

                }
                //总线型
                else if (kk == 2) {
                    if (j >= 1 && j <= 10) {
                        canvas.drawLine(origin.ZigbeeStellate[j - 1][0], origin.ZigbeeStellate[j - 1][1], origin.ZigbeeStellate[j][0], origin.ZigbeeStellate[j][1], paint);
                    }
                    if (j == 11) {
                        canvas.drawLine(origin.ZigbeeStellate[1][0], origin.ZigbeeStellate[1][1], origin.ZigbeeStellate[j][0], origin.ZigbeeStellate[j][1], paint);
                    }
                    if (j == 12) {
                        canvas.drawLine(origin.ZigbeeStellate[3][0], origin.ZigbeeStellate[3][1], origin.ZigbeeStellate[j][0], origin.ZigbeeStellate[j][1], paint);
                    }
                    if (j == 13 ) {
                        canvas.drawLine(origin.ZigbeeStellate[7][0], origin.ZigbeeStellate[7][1], origin.ZigbeeStellate[j][0], origin.ZigbeeStellate[j][1], paint);
                    }
                    if (j == 14 ) {
                        canvas.drawLine(origin.ZigbeeStellate[8][0], origin.ZigbeeStellate[8][1], origin.ZigbeeStellate[j][0], origin.ZigbeeStellate[j][1], paint);
                    }
                    if (j == 15 ) {
                        canvas.drawLine(origin.ZigbeeStellate[9][0], origin.ZigbeeStellate[9][1], origin.ZigbeeStellate[j][0], origin.ZigbeeStellate[j][1], paint);
                    }
                    if (j == 16 ) {
                        canvas.drawLine(origin.ZigbeeStellate[10][0], origin.ZigbeeStellate[10][1], origin.ZigbeeStellate[j][0], origin.ZigbeeStellate[j][1], paint);
                    }
                }
                /**
                 * 网状图
                 * */
                else {
                    if (j == 1) {
                        for (int l = 0; l < 9; l++) {
                            canvas.drawLine(origin.ZigbeeStellate[0][0], origin.ZigbeeStellate[0][1], origin.ZigbeeStellate[l][0], origin.ZigbeeStellate[l][1], paint);
                        }
                        canvas.drawLine(origin.ZigbeeStellate[j][0], origin.ZigbeeStellate[j][1], origin.ZigbeeStellate[8][0], origin.ZigbeeStellate[8][1], paint);
                    }
                    if (j >= 2 && j <= 8) {
                        canvas.drawLine(origin.ZigbeeStellate[j - 1][0], origin.ZigbeeStellate[j - 1][1], origin.ZigbeeStellate[j][0], origin.ZigbeeStellate[j][1], paint);
                    }
                    if (j == 16) {
                        canvas.drawLine(origin.ZigbeeStellate[j][0], origin.ZigbeeStellate[j][1], origin.ZigbeeStellate[7][0], origin.ZigbeeStellate[7][1], paint);
                        canvas.drawLine(origin.ZigbeeStellate[j][0], origin.ZigbeeStellate[j][1], origin.ZigbeeStellate[8][0], origin.ZigbeeStellate[8][1], paint);
                        canvas.drawLine(origin.ZigbeeStellate[j][0], origin.ZigbeeStellate[j][1], origin.ZigbeeStellate[1][0], origin.ZigbeeStellate[1][1], paint);
                    }
                    if (j == 14) {
                        canvas.drawLine(origin.ZigbeeStellate[j][0], origin.ZigbeeStellate[j][1], origin.ZigbeeStellate[5][0], origin.ZigbeeStellate[5][1], paint);
                        canvas.drawLine(origin.ZigbeeStellate[j][0], origin.ZigbeeStellate[j][1], origin.ZigbeeStellate[7][0], origin.ZigbeeStellate[7][1], paint);
                        canvas.drawLine(origin.ZigbeeStellate[j][0], origin.ZigbeeStellate[j][1], origin.ZigbeeStellate[6][0], origin.ZigbeeStellate[6][1], paint);
                    }
                    if (j == 12) {
                        canvas.drawLine(origin.ZigbeeStellate[j][0], origin.ZigbeeStellate[j][1], origin.ZigbeeStellate[3][0], origin.ZigbeeStellate[3][1], paint);
                        canvas.drawLine(origin.ZigbeeStellate[j][0], origin.ZigbeeStellate[j][1], origin.ZigbeeStellate[4][0], origin.ZigbeeStellate[4][1], paint);
                        canvas.drawLine(origin.ZigbeeStellate[j][0], origin.ZigbeeStellate[j][1], origin.ZigbeeStellate[5][0], origin.ZigbeeStellate[5][1], paint);
                    }
                    if (j == 10) {
                        canvas.drawLine(origin.ZigbeeStellate[j][0], origin.ZigbeeStellate[j][1], origin.ZigbeeStellate[1][0], origin.ZigbeeStellate[1][1], paint);
                        canvas.drawLine(origin.ZigbeeStellate[j][0], origin.ZigbeeStellate[j][1], origin.ZigbeeStellate[2][0], origin.ZigbeeStellate[2][1], paint);
                        canvas.drawLine(origin.ZigbeeStellate[j][0], origin.ZigbeeStellate[j][1], origin.ZigbeeStellate[3][0], origin.ZigbeeStellate[3][1], paint);
                    }
                    if (j == 9) {
                        canvas.drawLine(origin.ZigbeeStellate[9][0], origin.ZigbeeStellate[9][1], origin.ZigbeeStellate[16][0], origin.ZigbeeStellate[16][1], paint);
                    }
                    if (j >= 10 && j <= 16) {
                        canvas.drawLine(origin.ZigbeeStellate[j - 1][0], origin.ZigbeeStellate[j - 1][1], origin.ZigbeeStellate[j][0], origin.ZigbeeStellate[j][1], paint);

                    }


                }

            }

        }

    }

    /**
     * 移动线程
     */
    class MoveTo extends Thread {
        @Override
        public void run() {
            try {
                while (true) {
                    Thread.sleep(10);
                    for (int j = 0; j < end.length; j++) {
                        if (origin.ZigbeeStellate[j][0] != end[j][0]) {
                            if (origin.ZigbeeStellate[j][0] > end[j][0]) {
                                origin.ZigbeeStellate[j][0]--;
                            } else if (origin.ZigbeeStellate[j][0] < end[j][0]) {
                                origin.ZigbeeStellate[j][0]++;
                            }
                        }
                        if (origin.ZigbeeStellate[j][1] != end[j][1]) {
                            if (origin.ZigbeeStellate[j][1] > end[j][1]) {
                                origin.ZigbeeStellate[j][1]--;
                            } else if (origin.ZigbeeStellate[j][1] < end[j][1]) {
                                origin.ZigbeeStellate[j][1]++;
                            }
                        }
                        handler.sendEmptyMessage(0x789);
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 子节点移动线程
     */
    class ChildMove extends Thread {

        @Override
        public void run() {
            while (true) {
                try {
                    Thread.sleep(45);
                    du++;
                    if (du == 360) {
                        du = 0;
                    }
//                    for (int j = 0; j <end.length ; j++) {
//                        int num=new Random().nextInt(2);
//                        //0  向左  1  向右     2  向上    3  向下
//                            if (origin.ZigbeeStellate[j][0] == end[j][0]) {
//                                if (origin.ZigbeeStellate[j][1] == end[j][1]) {
//                                    switch (num){
//                                        case 0:
//
//                                        case 1:
//                                            Log.e("7788qq",num+"");
//                                            if(origin.ZigbeeStellate[j][0]<=origin.ZigbeeStellate[j][0]-10){
//                                                origin.ZigbeeStellate[j][0]++;
//                                            }
//                                            if(origin.ZigbeeStellate[j][0]>=origin.ZigbeeStellate[j][0]+10){
//                                                origin.ZigbeeStellate[j][0]--;
//                                            }
//                                            break;
//                                        case 2:
//                                        case 3:
//                                            if(origin.ZigbeeStellate[j][1]<=origin.ZigbeeStellate[j][1]-3){
//                                                origin.ZigbeeStellate[j][1]++;
//                                            } else if(origin.ZigbeeStellate[j][1]>=origin.ZigbeeStellate[j][1]+3){}
//                                            origin.ZigbeeStellate[j][1]--;
//                                            break;
//                                    }
//                                }
//                            }
//
//                        handler.sendEmptyMessage(0x789);
//                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 大小线程
     */
    class RingSize extends Thread {
        int[] i;
        int num;
        boolean[] bool;
        String[] level;

        public RingSize(int num, int[] i, boolean[] bool, String[] level) {
            this.i = i;
            this.num = num;
            this.bool = bool;
            this.level = level;
        }

        @Override
        public void run() {

            try {
                while (true) {
                    Thread.sleep(70);
                    if (bool[num] == false) {
                        if (i[num] <= Integer.parseInt(level[num]) + 10 + save[num]) {
                            i[num] += 1;
                        } else if (i[num] == Integer.parseInt(level[num]) + 10 + 1 + save[num]) {
                            bool[num] = true;
                        }
                        handler.sendEmptyMessage(0x123);

                    } else if (bool[num] == true) {
                        if (i[num] >= 0) {
                            i[num] -= 1;

                        } else if (i[num] == -1) {
                            bool[num] = false;
                        }
                        handler.sendEmptyMessage(0x123);
                    }

                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
